// AFAppDotNetAPIClient.h
//
// Copyright (c) 2012 Mattt Thompson (http://mattt.me/)
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import "WeAppHttpManager.h"
#import "themeModel.h"
#import "LabelModel.h"
#import "Common.h"
#import "CommentsModel.h"
#import "App_BannerModel.h"
#import "productInfoModel.h"
#import "SearchModel.h"
#import "ProductDetailModel.h"
#import "ApplyVersionModel.h"
#import "UserModel.h"
#import "CarlistModel.h"
#import "carDetailModel.h"
#import "ASIHTTPRequest.h"
#import "AudioButton.h"

//static NSString * const WeAppBaseURLString = @"http://120.26.47.159:8080/Micro_application/service/";
//测试时，
static NSString * const homepage=@"http://121.40.236.85:8080/birdie/";
//正式发布时，
//static NSString * const homepage=@"http://192.168.0.145:8080/birdie/";
////图片前缀测试时，
//static NSString * const imgBassURLString=@"http://konvy.dongshihuafeng.com/Konvy/static/";
////图片前缀正式发布时，
////static NSString * const imgBassURLString=@"http://www.konvy.com/static/";
@implementation WeAppHttpManager

+ (instancetype)sharedClient {
    static WeAppHttpManager *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[WeAppHttpManager alloc] initWithBaseURL:[NSURL URLWithString:homepage]];
        _sharedClient.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    });
    
    return _sharedClient;
}

-(void)getHttpMethordWithParameter:(NSDictionary *)parameter doneBlock:(void (^)(NSArray *, NSError *))block{
    [self GET:@"stream/0/posts/stream/global" parameters:parameter success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"responseObject==%@",responseObject);
        NSArray *arr = [NSArray arrayWithObjects:[NSNumber numberWithInteger:11], nil];
        block(arr,nil);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        block([NSArray array],error);
    }];
}

/* 首页
 *http://homepage/index.php
 */
-(void)getHomeDataPath:(NSString *)path WithDoneBlock:(void(^) (NSString *App_Html_BannerString, NSArray *App_BannerArr, NSArray *App_Circle_BannerArr, NSArray *Recommend_BrandsArr))block errorBlock:(void (^) (NSError *error))eBlock
{
    [self GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSString *App_Html_BannerString=nil;
        NSArray *App_BannerArr = [NSArray array];
        NSArray *App_Circle_BannerArr = [NSArray array];
        NSArray *Recommend_BrandsArr = [NSArray array];
        
        DLog(@"responseObject=====%@",responseObject);
        
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([[responseObject allKeys] containsObject:@"data"]) {
                NSLog(@"%@",[responseObject objectForKey:@"data"]);
            /*解析kv*/
            if ([[[responseObject objectForKey:@"data"]allKeys] containsObject:@"App_Circle_Banner"]) {
                if ([[[responseObject objectForKey:@"data"] objectForKey:@"App_Circle_Banner"] isKindOfClass:[NSArray class]]) {
                    App_BannerArr = [App_BannerModel arrayOfModelsFromDictionaries:[[responseObject objectForKey:@"data"] objectForKey:@"App_Circle_Banner"]];
                }
            }
            /*解析品牌*/
            if ([[[responseObject objectForKey:@"data"] allKeys] containsObject:@"App_Banner"]) {
                if ([[[responseObject objectForKey:@"data"] objectForKey:@"App_Banner"] isKindOfClass:[NSArray class]]) {
                    App_Circle_BannerArr = [App_BannerModel arrayOfModelsFromDictionaries:[[responseObject objectForKey:@"data"] objectForKey:@"App_Banner"]];
                }
            }
            /*解析推荐*/
            if ([[[responseObject objectForKey:@"data"] allKeys] containsObject:@"Recommend_Brands"]) {
                if ([[[responseObject objectForKey:@"data"] objectForKey:@"Recommend_Brands"] isKindOfClass:[NSArray class]]) {
                    Recommend_BrandsArr = [App_BannerModel arrayOfModelsFromDictionaries:[[responseObject objectForKey:@"data"] objectForKey:@"Recommend_Brands"]];
                }
            }
            /*解析连接*/
            if ([[[responseObject objectForKey:@"data"] allKeys] containsObject:@"App_Html_Banner"]) {
//                    App_Html_BannerString=[NSString ]
                App_Html_BannerString=[[responseObject objectForKey:@"data"] objectForKey:@"App_Html_Banner"];
            }
            
            if (block) {
                block(App_Html_BannerString,App_BannerArr,App_Circle_BannerArr,Recommend_BrandsArr);
            }
            
            }
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        eBlock(error);
    }];
}

/* LB获取验证码
 * http:// 192.168.0.113:8080/birdie/user/getVerifyCode
 */
- (void)getVerifyCodePath:(NSString *)path WithParameter:(NSDictionary *)parameter WithDoneBlock:(void(^) (NSString *errCode,NSString *errMsg))block errorBlock:(void (^) (NSError *error))eBlock{
    [self GET:path parameters:parameter success:^(NSURLSessionDataTask *task, id responseObject) {
        NSString *errCode = nil;
        NSString *errMsg=nil;
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([[responseObject allKeys] containsObject:@"errCode"]) {
                errCode = [responseObject objectForKey:@"errCode"];
            }
            if ([[responseObject allKeys] containsObject:@"errMsg"]) {
                errMsg = [responseObject objectForKey:@"errMsg"];
            }
            if (block) {
                block(errCode,errMsg);
            }
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        eBlock(error);
    }];
}


/* 推荐商品列表
 *
 */
- (void)getRecommentProductListPath:(NSString *)path WithDoneBlock:(void(^) (NSArray *themeList))block errorBlock:(void (^) (NSError *error))eBlock{
    [self GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
//        KVModel
        NSLog(@"%@",responseObject);
        NSArray *themeList = [NSArray array];
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([[responseObject allKeys] containsObject:@"data"]) {
                if ([[responseObject objectForKey:@"data"] isKindOfClass:[NSArray class]]) {
                    themeList = [productInfoModel arrayOfModelsFromDictionaries:[responseObject objectForKey:@"data"]];
                }
            }
            if (block) {
                NSLog(@"themeList====%@",themeList);
                block(themeList);
            }
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        eBlock(error);
    }];
}

/* 搜索
 * http://konvy.dongshihuafeng.com/app/search.php
 */
- (void)getSearchResultListPath:(NSString *)path WithDoneBlock:(void(^) (NSArray *brandListArr,NSArray *categoryListArr,NSArray *efficacysListArr))block errorBlock:(void (^) (NSError *error))eBlock{
    [self GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *brandListArr = [NSArray array];
        NSArray *categoryListArr = [NSArray array];
        NSArray *efficacysListArr = [NSArray array];
    
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([[responseObject allKeys] containsObject:@"data"]) {
                if ([[[responseObject objectForKey:@"data"] objectForKey:@"brand"]isKindOfClass:[NSArray class]]) {
                    brandListArr = [SearchModel arrayOfModelsFromDictionaries:[[responseObject objectForKey:@"data"] objectForKey:@"brand"]];
                }
            }
            if ([[responseObject allKeys] containsObject:@"data"]) {
                if ([[[responseObject objectForKey:@"data"] objectForKey:@"category"]isKindOfClass:[NSArray class]]) {
                    categoryListArr = [SearchModel arrayOfModelsFromDictionaries:[[responseObject objectForKey:@"data"] objectForKey:@"category"]];
                }
            }
            if ([[responseObject allKeys] containsObject:@"data"]) {
                if ([[[responseObject objectForKey:@"data"] objectForKey:@"efficacys"]isKindOfClass:[NSArray class]]) {
                    efficacysListArr = [SearchModel arrayOfModelsFromDictionaries:[[responseObject objectForKey:@"data"] objectForKey:@"efficacys"]];
                }
            }
            if (block) {
                block(brandListArr,categoryListArr,efficacysListArr);
            }
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        eBlock(error);
    }];
}

/* 获取商品详细信息
 * 说明：返回商品详细信息
 * 获取地址：http://konvy.dongshihuafeng.com/app/team.php
 * 获取方式：http get
 */
- (void)getProductDetailPath:(NSString *)path WithDoneBlock:(void(^) (ProductDetailModel *productDetailModel))block errorBlock:(void (^) (NSError *error))eBlock{
    [self GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        [self GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
            DLog(@"%@",responseObject);
            ProductDetailModel *productDetailModel=nil;
            if ([[responseObject objectForKey:@"result"] isEqualToString:@"success"] ) {
                                if ([[responseObject objectForKey:@"data"] isKindOfClass:[NSDictionary class]]) {
                    productDetailModel = [[ProductDetailModel alloc] initWithDictionary:[responseObject objectForKey:@"data"] error:nil];
                    if (block) {
                        block(productDetailModel);
                    }
                }
            }else{
                DLog(@"获取商品详细信息失败");
            }
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            eBlock(error);
        }];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        eBlock(error);
    }];
}


/* 用户收藏列表
 * http://192.168.0.200:8080/Micro_application/service/apply/getApplyByuserId?userId=e23e5c98-0308-40f9-9464-0bb620e463af
*/
- (void)getUserCollectionsPath:(NSString *)path WithDoneBlock:(void(^) (NSArray *UserCollections))block errorBlock:(void (^) (NSError *error))eBlock{
    [self GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *UserCollections=nil;
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([[responseObject allKeys] containsObject:@"data"]) {
                if ([[responseObject objectForKey:@"data"] isKindOfClass:[NSArray class]]) {
                    UserCollections = [KVModel arrayOfModelsFromDictionaries:[responseObject objectForKey:@"data"]];
                }
            }
                if (block) {
                    block(UserCollections);
                }
            }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        eBlock(error);
    }];

 }

/* 按时间排序
 * http://localhost:8080/Micro_application/service/apply/getDescCreateTime?pageIndex=0&pageSize=20
 */
- (void)getDescCreateTimePath:(NSString *)path WithDoneBlock:(void(^) (NSArray *DescCreateTimeArray))block errorBlock:(void (^) (NSError *error))eBlock{
    [self GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *DescCreateTimeArray=nil;
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([[responseObject allKeys] containsObject:@"data"]) {
                if ([[responseObject objectForKey:@"data"] isKindOfClass:[NSArray class]]) {
                    DescCreateTimeArray = [KVModel arrayOfModelsFromDictionaries:[responseObject objectForKey:@"data"]];
                }
            }
            if (block) {
                block(DescCreateTimeArray);
            }
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        eBlock(error);
    }];
    
}

/*
 * 根据访问量排序
 http://localhost:8080/Micro_application/service/apply/getDescNum?pageIndex=0&pageSize=20
 */
- (void)getDescNumPath:(NSString *)path WithDoneBlock:(void(^) (NSArray *DescNumArray))block errorBlock:(void (^) (NSError *error))eBlock{
    [self GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *DescNumArray=nil;
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([[responseObject allKeys] containsObject:@"data"]) {
                if ([[responseObject objectForKey:@"data"] isKindOfClass:[NSArray class]]) {
                    DescNumArray = [KVModel arrayOfModelsFromDictionaries:[responseObject objectForKey:@"data"]];
                }
            }
            if (block) {
                block(DescNumArray);
            }
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        eBlock(error);
    }];
    
}
/* LB用户注册
 *http:// 192.168.0.113:8080/birdie/user/saveUser
 *请求方式
 *post
 *	请求示例
 *{
 *mobile:"18539407061",
 *verifyCode:”1234”,
 *username:”admin”,
 *password:”123465”
 *}
 */
- (void)postSaveUserPath:(NSString *)path WithParameter:(NSDictionary *)parameter WithDoneBlock:(void(^) (NSString *errCode,NSString *errMsg,UserModel *usermodle))block errorBlock:(void (^) (NSError *error))eBlock{
    [self POST:path parameters:parameter success:^(NSURLSessionDataTask *task, id responseObject) {
        DLog(@"responseObject=====%@",responseObject);
        NSString *errCode=nil;
        NSString *errMsg=nil;
        UserModel *usermodle=nil;
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([[responseObject allKeys]containsObject:@"errCode"]) {
                errCode=[responseObject objectForKey:@"errCode"];
                if ([[responseObject allKeys]containsObject:@"errMsg"]) {
                    errMsg=[responseObject objectForKey:@"errMsg"];
                }
                if ([[responseObject allKeys]containsObject:@"user"]) {
                    if ([errCode isEqualToString:@"0"]) {
                        usermodle = [[UserModel alloc] initWithDictionary:[responseObject objectForKey:@"user"] error:nil];
                    }
                }
                if (block) {
                    block(errCode,errMsg,usermodle);
                }
            }
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        eBlock(error);
    }];
}

 /* LB用户登录
  * http:// 192.168.0.113:8080/birdie/user/login
  {
  userName:”admin”,
  passWord:”123465”
  }
 */
- (void)postUserLoginPath:(NSString *)path WithParameter:(NSDictionary *)parameter WithDoneBlock:(void(^) (NSString *errCode,NSString *errMsg,UserModel *usermodle))block errorBlock:(void (^) (NSError *error))eBlock{
    [self POST:path parameters:parameter success:^(NSURLSessionDataTask *task, id responseObject) {
        DLog(@"responseObject=====%@",responseObject);
        NSString *errCode=nil;
        NSString *errMsg=nil;
        UserModel *usermodle=nil;
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([[responseObject allKeys]containsObject:@"errCode"]) {
                errCode=[responseObject objectForKey:@"errCode"];
                if ([[responseObject allKeys]containsObject:@"errMsg"]) {
                    errMsg=[responseObject objectForKey:@"errMsg"];
                }
                if ([[responseObject allKeys]containsObject:@"user"]) {
                    if ([errCode isEqualToString:@"0"]) {
                        usermodle = [[UserModel alloc] initWithDictionary:[responseObject objectForKey:@"user"] error:nil];
                    }
                }
                if (block) {
                    block(errCode,errMsg,usermodle);
                }
            }
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        eBlock(error);
    }];
}

//"errCode": 0,
//"id": "2ebe24f9-7c97-4590-a1ee-215577f41208",
//"title": "2002宝马",
//"brand": "宝马",
//"mileage": 0,
//"preSalePrice": 0,
//"dayPrice": 0,
//"carImageUrl": "http://www.baidu.com",
//"vehicleAge": 0,
//"dateOfProduction": "2002",
//"hitAPrice": false
//http:// 192.168.0.113:8080/birdie/car/findList?pageIndex=0&pageSize=5


- (void)getFindlist:(NSString *)path WithDoneBlock:(void(^) (NSArray *carlist))block errorBlock:(void (^) (NSError *error))eBlock{
    [self GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        DLog(@"%@",responseObject);
        NSArray *carlist = [[NSArray alloc]init];
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            
            if ([[responseObject allKeys] containsObject:@"data"]) {
                carlist=[CarlistModel arrayOfModelsFromDictionaries:[responseObject objectForKey:@"data"] error:nil];
                DLog(@"%@",carlist);
            }
            if (block) {
                block(carlist);
            }
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        eBlock(error);
    }];
}


//11.获取汽车详情
//¬	请求地址
//http://localhost:8080/birdie/car/get?id=6a817ec3-4057-4bdf-b00f-574c0661393c
//请求方式
//Get、POST
//¬	请求示例
//http://localhost:8080/birdie/car/get?id=6a817ec3-4057-4bdf-b00f-574c0661393c
//
//¬	请求参数说明

- (void)getCarDetailInfo:(NSString *)path WithDoneBlock:(void(^) (carDetailModel *model))block errorBlock:(void (^) (NSError *error))eBlock{
    [self GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        DLog(@"%@",responseObject);
        carDetailModel *model=nil;
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            model=[[carDetailModel alloc]initWithDictionary:responseObject error:nil];
            DLog(@"%@",model);
            if (block) {
                block(model);
            }
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        eBlock(error);
    }];
}



/* 密码重置
 * http://192.168.0.200:8080/Micro_application/service/user/resetPassword
 */
- (void)postResetPasswordPath:(NSString *)path WithParameter:(NSDictionary *)parameter WithDoneBlock:(void(^) (NSString * errCode,NSString * errMsg ))block errorBlock:(void (^) (NSError *error))eBlock{
    [self POST:path parameters:parameter success:^(NSURLSessionDataTask *task, id responseObject) {
        DLog(@"responseObject=====%@",responseObject);
        NSString * errCode=nil;
        NSString * errMsg = nil;
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            errCode=[responseObject objectForKey:@"errCode"];
            if ([[responseObject objectForKey:@"errCode"] integerValue]!=0) {
                errMsg=[responseObject objectForKey:@"errMsg"];
            }
            if (block) {
                block(errCode,errMsg);
            }
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        eBlock(error);
    }];
}

/* 应用详情
 * http://192.168.0.200:8080/Micro_application/service/apply/get?userId=98efc07e-8719-4dba-9c2b-f2e539b68217&applyId=6a78aa15-8d05-40db-ab2b-a4480c6ccf62
 */
- (void)getApplyDetailInfoPath:(NSString *)path WithDoneBlock:(void(^) (KVModel *detailModel))block errorBlock:(void (^) (NSError *error))eBlock{
    [self GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        KVModel *detailModel = nil;
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            
            detailModel = [[KVModel alloc] initWithDictionary:responseObject error:nil];
            
            if (block) {
                block(detailModel);
            }
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        eBlock(error);
    }];
}

/* 用户评论列表
 * http://192.168.0.200:8080/Micro_application/service/comments/getCommentsByapplyId?applyVersionId=e67bbc39-6ad5-4f1f-b504-4d46e28051db&applyId=cc35c33e-a373-4ecf-85e3-1c7bb503e4ac
 */
- (void)getCommentsListPath:(NSString *)path WithDoneBlock:(void(^) (NSArray *allCommentsList))block errorBlock:(void (^) (NSError *error))eBlock{
    [self GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSArray *allCommentsList = [NSArray array];
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([[responseObject allKeys] containsObject:@"data"]) {
//                if ([[responseObject objectForKey:@"data"] isKindOfClass:[NSArray class]]) {
//                    allCommentsList = [CommentModel arrayOfModelsFromDictionaries:[responseObject objectForKey:@"data"]];
//                }
            }
            if (block) {
                block(allCommentsList);
            }
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        DLog(@"error=====%@",[error localizedDescription]);
        eBlock(error);
    }];
}

/* 用户添加收藏接口
 * http://192.168.0.200:8080/Micro_application/service/collection/add
 */
- (void)postAddCollectionPath:(NSString *)path WithParameter:(NSDictionary *)parameter WithDoneBlock:(void(^) (NSString *errorCode))block errorBlock:(void (^) (NSError *error))eBlock{
    [self POST:path parameters:parameter success:^(NSURLSessionDataTask *task, id responseObject) {
        NSString *errorCode = nil;
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([[responseObject allKeys] containsObject:@"errCode"]) {
                errorCode = [responseObject objectForKey:@"errCode"];
            }
            if (block) {
                block(errorCode);
            }
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        eBlock(error);
    }];
}

/* 用户取消收藏
 * http://192.168.0.200:8080/Micro_application/service/collection/del?userId=98efc07e-8719-4dba-9c2b-f2e539b68217&applyId=6a78aa15-8d05-40db-ab2b-a4480c6ccf62
 */
- (void)getDeleateCollectionPath:(NSString *)path WithParameter:(NSDictionary *)parameter WithDoneBlock:(void(^) (NSString *errorCode))block errorBlock:(void (^) (NSError *error))eBlock{
    [self GET:path parameters:parameter success:^(NSURLSessionDataTask *task, id responseObject) {
        NSString *errorCode = nil;
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([[responseObject allKeys] containsObject:@"errCode"]) {
                errorCode = [responseObject objectForKey:@"errCode"];
            }
            if (block) {
                block(errorCode);
            }
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        eBlock(error);
    }];
}

/* 分类列表
 * http://192.168.0.200:8080/Micro_application/service/theme/getThemeALL
 */
- (void)getThemeAllPath:(NSString *)path WithDoneBlock:(void(^) (NSArray *themeList))block errorBlock:(void (^) (NSError *error))eBlock{
    
    [self GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *themeList = nil;
        
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([[responseObject allKeys] containsObject:@"data"]) {
                if ([[responseObject objectForKey:@"data"] isKindOfClass:[NSArray class]]) {
                    themeList = [themeModel arrayOfModelsFromDictionaries:[responseObject objectForKey:@"data"]];
                }
            }
            if (block) {
                block(themeList);
            }
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        eBlock(error);
    }];
}

/* 获取app信息
 * http://konvy.dongshihuafeng.com/app/sessionid.php
 */
- (void)getVersionPath:(NSString *)path WithDoneBlock:(void(^) (ApplyVersionModel *model))block errorBlock:(void (^) (NSError *error))eBlock{
    [self GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        ApplyVersionModel *model = nil;
        model=[[ApplyVersionModel alloc]initWithDictionary:responseObject error:nil];
        if (block) {
            block(model);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        eBlock(error);
    }];
}

/* 添加评论 POST
 * http://192.168.0.200:8080/Micro_application/service/comments/add
 */
- (void)postCommentsPath:(NSString *)path WithParameter:(NSDictionary *)parameter WithDoneBlock:(void(^) (NSString *errorCode))block errorBlock:(void (^) (NSError *error))eBlock{
    [self POST:path parameters:parameter success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSString *errorCode = nil;
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([[responseObject allKeys] containsObject:@"errCode"]) {
                errorCode = [responseObject objectForKey:@"errCode"];
            }
            if (block) {
                block(errorCode);
            }
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        eBlock(error);
    }];
}

/* 播放视频
 * 
 */
- (void)PlayVideoAction:(NSString *)path WithDoneBlock:(void(^) (NSURL *playUrl))block errorBlock:(void (^) (NSError *error))eBlock{
   __block unsigned long long Recordull;
    Recordull=0;
   __block BOOL isPlay;
    isPlay=NO;
    NSString *webPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Private Documents/Temp"];
    NSString *cachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Private Documents/Cache"];
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:cachePath])
    {
        [fileManager createDirectoryAtPath:cachePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
//    if ([fileManager fileExistsAtPath:[cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"vedio.MOV"]]]) {
//        MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:[cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"vedio.MOV"]]]];
//        [self presentMoviePlayerViewControllerAnimated:playerViewController];
//        block([NSURL fileURLWithPath:[cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"vedio.MOV"]]]);
//        videoRequest = nil;
//    }else{
        ASIHTTPRequest *request=[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:path]];
//        AudioButton *musicBt = (AudioButton *)[self.view viewWithTag:1];
//        [musicBt startSpin];
        //下载完存储目录
        [request setDownloadDestinationPath:[cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"vedio.MOV"]]];
        //临时存储目录
        [request setTemporaryFileDownloadPath:[webPath stringByAppendingPathComponent:[NSString stringWithFormat:@"vedio.MOV"]]];
        [request setBytesReceivedBlock:^(unsigned long long size, unsigned long long total) {
//            [musicBt stopSpin];
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setDouble:total forKey:@"file_length"];
            Recordull= size+Recordull;//Recordull全局变量，记录已下载的文件的大小
            NSLog(@"Recordull===%llu",Recordull);
            if (!isPlay) {
                block([NSURL URLWithString:@"http://127.0.0.1:12345/vedio.MOV"]);
                isPlay=YES;
            }
        }];
        //断点续载
        [request setAllowResumeForFileDownloads:YES];
        [request startAsynchronous];
//        [request clearDelegatesAndCancel];

//        videoRequest = request;
//    }

    
    }

////-  添加图片
//- (void)postImageUpload:(NSData*)fileData type:(NSString *)type WithDoneBlock:(void(^) (NSDictionary *responseObject))block errorBlock:(void (^) (NSError *error))eBlock{
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    // AFHTTPResponseSerializer就是正常的HTTP请求响应结果:NSData
//    // 当请求的返回数据不是JSON,XML,PList,UIImage之外,使用AFHTTPResponseSerializer
//    // 例如返回一个html,text...
//    //
//    // 实际上就是AFN没有对响应数据做任何处理的情况
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];
//    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//    
//    // formData是遵守了AFMultipartFormData的对象
//    [manager POST:FILEUPLOADSERVERURL parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//        // 将本地的文件上传至服务器
//        UserModel *model=[TMC objectForKey:userModel];
//        [formData appendPartWithFormData:[[model userId] dataUsingEncoding:NSUTF8StringEncoding] name:@"userid"];
//        [formData appendPartWithFormData:[[NSString stringWithFormat:@"%@",[TMC objectForKey:OPENUDID]] dataUsingEncoding:NSUTF8StringEncoding] name:@"deviceId"];
//        [formData appendPartWithFormData:[@"1" dataUsingEncoding:NSUTF8StringEncoding] name:@"type"];
//        NSString *strRandomName = [NSString stringWithFormat:@"iOS_uploadImage_%d.jpg",arc4random_uniform(10000)];
//        //        NSString *strRandomName=@".jpg";
//        [formData appendPartWithFileData:fileData name:@"image" fileName:strRandomName mimeType:@"application/octet-stream"];
//        
//    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
//        
//        NSLog(@"完成 %@", result);
//        NSData *jsonData = [result dataUsingEncoding:NSUTF8StringEncoding];
//        NSError *err;
//        NSDictionary *imgDic;
//        imgDic = [NSJSONSerialization JSONObjectWithData:jsonData
//                                                 options:NSJSONReadingMutableContainers
//                                                   error:&err];
//        NSLog(@"%@",imgDic);
//        if (block) {
//            block(imgDic);
//        }
//        
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"错误 %@", error.localizedDescription);
//    }];
//    
//}


@end
